package com.google.code.sip.ucenter.ws.resources;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.code.sip.ucenter.model.Notification;
import com.google.code.sip.ucenter.model.User;
import com.google.code.sip.ucenter.service.NotificationManager;
import com.google.code.sip.ucenter.service.UserManager;
import com.google.code.sip.ucenter.ws.WSResponse;
import com.google.code.sip.ucenter.ws.WSResponseStatus;
import com.google.code.sip.ucenter.ws.dto.NotificationDto;
import com.sun.jersey.spi.resource.Singleton;

@Component
@Path("/notice")
@Singleton
@Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public class NotificationResource extends AbstractResource {
	private final Log logger = LogFactory.getLog(getClass());
	@Autowired
	private NotificationManager notificationManager;
	@Autowired
	private UserManager userManager;

	@POST
	@Path("/get")
	public WSResponse<NotificationDto> getNotification(
			@FormParam("noticeId") String noticeId) {
		assert (null != noticeId) : "noticeId cann't be null.";
		try {
			Notification notice = notificationManager.getNotification(noticeId);
			if (notice != null) {
				NotificationDto dto = toDto(notice);
				return new WSResponse<NotificationDto>(dto);
			} else {
				return new WSResponse<NotificationDto>(new WSResponseStatus(-1,
						"该消息不存在"));
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new WSResponse<NotificationDto>(
					WSResponseStatus.SERVER_ERROR);
		}
	}

	@POST
	@Path("/send")
	public WSResponse<NotificationDto> getNotificationByName(
			Notification notification) {
		return new WSResponse<NotificationDto>();

	}

	@POST
	@Path("/list")
	public WSResponse<NotificationDto> getNotifications(
			@FormParam("userId") String userId) {
		assert (null != userId) : "userId cann't be null.";
		try {
			User user = userManager.getUser(userId);
			if (user == null) {
				return new WSResponse<NotificationDto>(new WSResponseStatus(-1,
						"该用户不存在。"));
			}
			List<NotificationDto> notices = toDto(notificationManager
					.getNotificationsByUser(user));
			return new WSResponse<NotificationDto>(notices);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new WSResponse<NotificationDto>(
					WSResponseStatus.SERVER_ERROR);
		}
	}

	@POST
	@Path("/delete")
	public WSResponse<String> deleteNotification(
			@FormParam("noticeId") String noticeId) {
		try {
			notificationManager.deleteNotification(noticeId);
			return new WSResponse<String>();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new WSResponse<String>(WSResponseStatus.SERVER_ERROR);
		}
	}

	protected List<NotificationDto> toDto(List<Notification> notices) {
		List<NotificationDto> list = new ArrayList<NotificationDto>(notices
				.size());
		if (notices != null && notices.isEmpty()) {
			Iterator<Notification> iter = notices.iterator();
			while (iter.hasNext()) {
				NotificationDto dto = toDto(iter.next());
				list.add(dto);
			}
		}
		return list;
	}

	protected NotificationDto toDto(Notification notification) {
		NotificationDto dto = new NotificationDto();
		BeanUtils.copyProperties(notification, dto);
		return dto;
	}
}
